// 1、编程实现对数据库表操作
//   （1）通过键盘输入数据，插入数据库表中
//   （2）通过键盘输入学员编号，删除该学员信息
//   （3）通过键盘输入学员编号，将其成绩改为89

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>

struct student
{
    int code;
    char name[10];
    int age;
};

/*int main()
{
    char sql[100] = {0};
    MYSQL *res = NULL, *connect = NULL;
    connect = mysql_init(NULL);
    res = mysql_real_connect(connect, "localhost", "root", "","student", 0, NULL, 0);
    if (res == NULL)
    {
        printf("%s\n", mysql_error(connect));
        exit(-1);
    }
    printf("connect is success\n");
    int ret = 0;
    struct student stu;
    memset(&stu, 0, sizeof(struct student));
    printf("please input code name age:\n");
    scanf("%d %s %d", &stu.code, stu.name, &stu.age);
    sprintf(sql, "insert into student values(%d,'%s',%d)",stu.code, stu.name, stu.age);

    ret = mysql_query(connect, sql);
    if (ret != 0)
    {
        printf("%s\n", mysql_error(connect));
        exit(-1);
    }
    mysql_close(connect);
    return 0;
}*/

// （2）通过键盘输入学员编号，删除该学员信息

/*int main()
{
    char sql[100] = {0};
    MYSQL *res = NULL, *connect = NULL;
    connect = mysql_init(NULL);
    res = mysql_real_connect(connect, "localhost", "root", "", "student", 0, NULL, 0);
    if (res == NULL)
    {
        printf("%s\n", mysql_error(connect));
        exit(-1);
    }
    printf("connect is success\n");
    int ret = 0;
    struct student stu;
    memset(&stu, 0, sizeof(struct student));
    printf("please input code :\n");
    scanf("%d", &stu.code);
    sprintf(sql, "delete from student where code=%d", stu.code);
    
    ret = mysql_query(connect, sql);
    if (ret != 0)
    {
        printf("%s\n", mysql_error(connect));
        exit(-1);
    }
    mysql_close(connect);
    return 0;
}*/

//   （3）通过键盘输入学员编号，将其成绩改为89

int main()
{
    char sql[100] = {0};
    MYSQL *res = NULL, *connect = NULL;
    connect = mysql_init(NULL);
    res = mysql_real_connect(connect, "localhost", "root", "", "student", 0, NULL, 0);
    if (res == NULL)
    {
        printf("%s\n", mysql_error(connect));
        exit(-1);
    }
    printf("connect is success\n");
    int ret = 0;
    struct student stu;
    memset(&stu, 0, sizeof(struct student));
    printf("please input code :\n");
    scanf("%d", &stu.code);
    sprintf(sql, "update subject set score=89 where code=%d", stu.code);
    
    ret = mysql_query(connect, sql);
    if (ret != 0)
    {
        printf("%s\n", mysql_error(connect));
        exit(-1);
    }
    mysql_close(connect);
    return 0;
}